﻿namespace IP_Package.GUI
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    /// <summary>
    /// calculation form
    /// </summary>
    public partial class Calculation_form : Form
    {
        /// <summary>
        /// sub two pic operation
        /// </summary>
        private IPPackage.ImageAlgebra.TwoImgOperation subImg = new IPPackage.ImageAlgebra.TwoImgOperation();

        /// <summary>
        /// add two image
        /// </summary>
        private IPPackage.ImageAlgebra.TwoImgOperation addImg = new IPPackage.ImageAlgebra.TwoImgOperation();

        /// <summary>
        /// the image two in operation
        /// </summary>
        private IPPackage.ImageOperation.Open img2 = new IPPackage.ImageOperation.Open();

        /// <summary>
        /// buffer of orignal image
        /// </summary>
        private Color[,] bufferOfImage;

        /// <summary>
        /// flag to known if it save in orginal or not
        /// </summary>
        private bool saveOrNot;

        /// <summary>
        /// result image
        /// </summary>
        private Bitmap resultImg;

        /// <summary>
        /// Initializes a new instance of the <see cref="Calculation_form"/> class.
        /// </summary>
        public Calculation_form()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Gets or sets the img21.
        /// </summary>
        /// <value>
        /// The img21.
        /// </value>
        public IPPackage.ImageOperation.Open Img21
        {
            get { return this.img2; }
            set { this.img2 = value; }
        }

        /// <summary>
        /// Gets or sets a value indicating whether [save or not1].
        /// </summary>
        /// <value>
        ///   <c>true</c> if [save or not1]; otherwise, <c>false</c>.
        /// </value>
        public bool SaveOrNot1
        {
            get { return this.saveOrNot; }
            set { this.saveOrNot = value; }
        }

        /// <summary>
        /// Gets or sets the buffer of image.
        /// </summary>
        /// <value>
        /// The buffer of image.
        /// </value>
        public Color[,] BufferOfImage
        {
            get { return this.bufferOfImage; }
            set { this.bufferOfImage = value; }
        }

        /// <summary>
        /// Gets or sets the result img.
        /// </summary>
        /// <value>
        /// The result img.
        /// </value>
        public Bitmap ResultImg
        {
            get { return this.resultImg; }
            set { this.resultImg = value; }
        }

        /// <summary>
        /// Sets the info of orignal image.
        /// </summary>
        /// <param name="image">The orginal image.</param>
        /// <param name="width">The width of orignal image.</param>
        /// <param name="height">The height of orignal image.</param>
        /// <param name="bufferOfImage">The buffer of orignal image.</param>
        internal void SetInfo(Image image, string width, string height, Color[,] bufferOfImage)
        {
            this.picOrg.Image = image;
            this.LabWdth.Text = width;
            this.LabHth.Text = height;
            this.bufferOfImage = bufferOfImage;
        }

        /// <summary>
        /// Handles the Click event of the Button2 control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void Button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string opendFileName = openFileDialog1.FileName;
                Bitmap image2 = this.img2.OpenImage(opendFileName);
                this.pictdSecondImg.Image = image2;
            }
        }

        /// <summary>
        /// Handles the CheckedChanged event of the RadioSub control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void RadioSub_CheckedChanged(object sender, EventArgs e)
        {
            if (this.radioSub.Checked == true)
            {
                try
                {
                    this.subImg.BufferOfImage = this.BufferOfImage;
                   this.resultImg = this.subImg.Sub(this.img2.BufferOfImage);
                    this.pictureBox3.Image = this.resultImg;
                }
                catch
                {
                    MessageBox.Show("please make sure your data if no error operation fail");
                }
            }
        }

        /// <summary>
        /// Handles the CheckedChanged event of the RadioAdd control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void RadioAdd_CheckedChanged(object sender, EventArgs e)
        {
            if (this.radioAdd.Checked == true)
            {
                FractionForm newFra = new FractionForm();
                newFra.ShowDialog();
                float frac = newFra.Fractor_value;
                this.addImg.BufferOfImage = this.BufferOfImage;
                try
                {
                    this.resultImg = this.addImg.Add(this.img2.BufferOfImage, frac);
                    this.pictureBox3.Image = this.resultImg;
                }
                catch
                {
                    MessageBox.Show("please make sure your data if no error operation fail");
                }
            }
        }

        /// <summary>
        /// Handles the Click event of the BTOK control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void BTOK_Click(object sender, EventArgs e)
        {
            if (this.resultImg == null)
            {
                if (radioAdd.Checked == true)
                {
                    FractionForm newFra = new FractionForm();
                    newFra.ShowDialog();
                    float frac = newFra.Fractor_value;
                    this.addImg.BufferOfImage = this.BufferOfImage;
                    try
                    {
                        this.resultImg = this.addImg.Add(this.img2.BufferOfImage, frac);
                        this.pictureBox3.Image = this.resultImg;
                    }
                    catch
                    {
                        MessageBox.Show("please make sure your data if no error operation fail");
                    }

                    this.bufferOfImage = this.addImg.NewBufferOfImage;
                }
                else
                {
                    if (radioSub.Checked == true)
                    {
                        try
                        {
                            this.subImg.BufferOfImage = this.BufferOfImage;
                            this.resultImg = this.subImg.Sub(this.img2.BufferOfImage);
                            this.pictureBox3.Image = this.resultImg;
                        }
                        catch
                        {
                            MessageBox.Show("please make sure your data if no error operation fail");
                        }
                    }
        
                    this.bufferOfImage = this.subImg.NewBufferOfImage;
                }
            }

            this.saveOrNot = true;
            this.Close();
        }

        /// <summary>
        /// Handles the Click event of the BtCnacel control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void BtCnacel_Click(object sender, EventArgs e)
        {
            this.saveOrNot = false;
            this.Close();
        }
    }
}
